package top.lixb.libcommon.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.vondear.rxtool.RxBarTool;
import com.vondear.rxtool.RxImageTool;
import com.vondear.rxui.view.dialog.RxDialogLoading;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import top.lixb.libcommon.R;
import top.lixb.libcommon.databinding.ActivityBaseBinding;
import top.lixb.libcommon.log.L;
import top.lixb.libcommon.net.CommonCallback;
import top.lixb.libcommon.net.INet;
import top.lixb.libcommon.net.NetApiHelper;
import top.lixb.libcommon.utils.StatusBarUtil;

public class BaseActivity<T extends ViewDataBinding> extends SwipeBackActivity implements INet {
    protected Toolbar mToolbar;
    protected TextView mTitle;
    protected RxDialogLoading mRxDialogLoading;
    protected View mStatusbar;
    protected ActivityBaseBinding mBaseBinding;
    protected T mContentBinding;
    protected ImageView mIvRight;
    protected View mToolbarDivider;
    protected TextView mTvRight;
    protected ImageView mIvLeft;
    private AppBarLayout mAppBarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBaseBinding = DataBindingUtil.setContentView(this, R.layout.activity_base);
        initToolbar();
        setStatusBar();
        mBaseBinding.executePendingBindings();
        setStatusbarBackgroundColor(getResources().getColor(R.color.color_status_bar));
    }

    private void initToolbar() {
        mAppBarLayout = (AppBarLayout) findViewById(R.id.base_appbar);
        mToolbar = mBaseBinding.getRoot().findViewById(R.id.base_toolbar);
        mTitle = mBaseBinding.getRoot().findViewById(R.id.base_tv_title);
        mToolbarDivider = findViewById(R.id.toolbar_divider);
        mTvRight = mToolbar.findViewById(R.id.tv_right);
        mIvRight = mBaseBinding.getRoot().findViewById(R.id.base_iv_rightmenu);
        mIvLeft = mToolbar.findViewById(R.id.base_iv_back);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }
        if (getSupportActionBar() != null) {
            // Enable the Up button
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(false);

        }
        mToolbar.findViewById(R.id.base_iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mStatusbar = mBaseBinding.getRoot().findViewById(R.id.base_statusbar);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mStatusbar.getLayoutParams();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT ) {
            layoutParams.height = 0;
        }else {
            layoutParams.height = StatusBarUtil.getStatusBarHeight(this);
        }
        mStatusbar.setLayoutParams(layoutParams);

    }

    protected void showToolbarDivider() {
        if (null != mToolbarDivider) {
            mToolbarDivider.setVisibility(View.VISIBLE);
        }
    }

    private void setStatusBar() {
        StatusBarUtil.setStatusBarTranslucent(this,true);
    }

    protected void setStatusbarBackgroundColor(int color) {
        if (null != mStatusbar) {
            if (color == Color.WHITE && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                return;
            }
            mStatusbar.setBackgroundColor(color);
        }
    }


    @Override
    public void setContentView(int layoutId) {
        if (layoutId != R.layout.activity_base) {
            try { //解决布局不是databinding布局
                mContentBinding = DataBindingUtil.inflate(LayoutInflater.from(this), layoutId, mBaseBinding.baseContentView, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            super.setContentView(layoutId);
        }
    }

    @Override
    public void setContentView(View view) {
        mBaseBinding.baseContentView.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        try {
            mContentBinding = DataBindingUtil.findBinding(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected void setNoTitle() {
        mToolbar.setVisibility(View.GONE);
    }

    protected void setTitle(String title) {
        mTitle.setText(title);
    }

    protected <T extends View> T f(int id) {
        return (T)findViewById(id);
    }

    @Override
    public void performGetRequest(String baseUrl,String url, Map<String, String> params, CommonCallback callback) {
        NetApiHelper.createNetApi(baseUrl)
                .commonGetRequest(url, params, NetApiHelper.getCommonHeader())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback)
        ;
    }

    @Override
    public void performPostRequest(String baseUrl,String url, Map<String, String> params, CommonCallback callback) {
        NetApiHelper.createNetApi(baseUrl)
                .commonPostRequest(url, params, NetApiHelper.getCommonHeader())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callback)
        ;
    }


    @Override
    protected void onStop() {
        super.onStop();
        hideLoading();
    }



    protected RxDialogLoading getLoadingDialog() {
        if (null == mRxDialogLoading) {
            mRxDialogLoading = new RxDialogLoading(this);
            mRxDialogLoading.setContentView(R.layout.dialog_loading);
        }
        return mRxDialogLoading;
    }
    protected void showLoading() {
//        if (!getLoadingDialog().isShowing()) {
//            getLoadingDialog().show();
//        }
    }

    protected void hideLoading() {
//        if (getLoadingDialog().isShowing()) {
//            getLoadingDialog().dismiss();
//        }
    }


}
